package com.cupk.carManageMaster.core.serviceImpl;

import com.cupk.carManageMaster.common.domain.DispatchCar;
import com.cupk.carManageMaster.common.domain.Driver;
import com.cupk.carManageMaster.common.domain.ResponseBase;
import com.cupk.carManageMaster.common.domain.User;
import com.cupk.carManageMaster.common.mapper.DriverMapper;
import com.cupk.carManageMaster.core.service.DriverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 名称DriverServiceImpl
 * 描述
 *
 * @version 1.0
 * @author:wanghao
 * @datatime:2023,06,03,19,16
 */
@Service
@Transactional
public class DriverServiceImpl extends BaseApiServiceImpl implements DriverService {
    @Autowired
    DriverMapper driverMapper;

    @Override
    public ResponseBase getDriverInfoByNum(String jobNumber) {
        String i="Succeed";
        try {
            User user = driverMapper.getDriverInfoByNum(jobNumber);
            Integer userId =  user.getId();
            Driver driver = driverMapper.findDriverByUserId(userId);
            Integer driverId = driver.getId();
            Integer typeCarId = driver.getTypeCarId();
            Integer drivingExperience = driver.getDrivingExperience();
            String typeCar = driverMapper.findTypeCarById(typeCarId);
            user.setDriverId(driverId);
            user.setDrivingExperience(drivingExperience);
            user.setTypeCar(typeCar);
        }catch (Exception e){
             i = "Wrong";
        }
        if (i.equals("Succeed")){
            return setResultSuccess(driverMapper.getDriverInfoByNum(jobNumber));
        }else {
            return setResultError("False");
        }
    }

    @Override
    public ResponseBase getJobsById(Integer id, Integer status) {
        String i = "Succeed";
        try{
            List<Integer> carIds = driverMapper.findCarIdsById(id);
            for (int j = 0;j < carIds.size();j++)
            {
                String carNumber = driverMapper.findCarNumbersById(carIds.get(j));
                driverMapper.getJobsById(id, status).get(j).addCarNumber(carNumber);
            }
        }catch (Exception e){
            i = "Wrong";
        }
        if (i.equals("Succeed")){
            return setResultSuccess(driverMapper.getJobsById(id, status));
        }else {
            return setResultError("False");
        }
    }

    @Override
    public ResponseBase changeWork(Integer id, Integer status, Long scheduleEndTime) {
        Map<String, String> errorMsg = new HashMap<>();
        try {
            Integer CarId = driverMapper.findCarIdById(id);
            Integer DriverId = driverMapper.findDriverIdById(id);
            Integer messageMileage = driverMapper.findMessageCarMileageById(CarId);
            Integer dispatchMileage = driverMapper.findDispatchMileageById(id);
            Integer mileage = messageMileage+dispatchMileage;
            driverMapper.updateMessageCarMileageById(mileage, CarId);
            Date date = new Date(scheduleEndTime);
            driverMapper.changeWork(id, status, date);
            driverMapper.updateDriverStatusById(DriverId, status);
            if (status == 0) {
                driverMapper.updateMessageCarWorkingById(true, CarId);
            } else if (status == 2) {
                driverMapper.updateMessageCarWorkingById(false, CarId);
            }
        } catch (Exception e) {
            errorMsg.put("errorMsg", e.toString());
        }
        if (errorMsg.isEmpty()) {
            return setResultSuccess(driverMapper.findDispatchCarById(id));
        } else {
            return setResultError("False");
        }
    }


    @Override
    public ResponseBase insertHelp(Integer isEmergent, Long alertTime, Integer helpSeeker) {
        String i = "Succeed";
        try {
            Date date = new Date(alertTime);
            driverMapper.insertHelp(isEmergent, date, helpSeeker);
        }catch (Exception e){
            i = "Wrong";
        }
        if (i.equals("Succeed")){
            return setResult(200,"Succeed","Succeed");
        }else {
            return setResultError("False");
        }
    }
}
